home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6053 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  74 lines

  1. Path: rain.fr!world-net!usenet
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: function pointer in C++
  5. Date: Sun, 11 Feb 1996 10:19:23 +0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Message-ID: <VA.0000002d.0002eabc@fred>
  8. References: <4f7rd5$6ha@transfer.stratus.com>
  9. Reply-To: lachass@worldnet.fr
  10. NNTP-Posting-Host: client42.sct.fr
  11. X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
  12.  
  13. I don't think there's a simple elegant solution. May be several 
  14. templates like:
  15.  
  16. template <class T1>
  17. struct fn1
  18. {
  19.   typedef void (*ptr)(T1);
  20. };
  21.  
  22. template <class T1, class T2>
  23. struct fn2
  24. {
  25.   typedef void (*ptr)(T1, T2);
  26. };
  27.  
  28. Then you can declare a void (*)(int, char **) using:
  29.  
  30. fn2<int,char**>::ptr foo;
  31.  
  32. (When template typedefs will be available, simpler solution)
  33.  
  34. More complex template but simpler declarations
  35.  
  36. template <class T1>
  37. class pfn1
  38. {
  39. public:
  40.   typedef void (*pfn)(T1);
  41.   pfn1() {}
  42.   pfn1(pfn ptr) : _ptr(ptr) {}
  43.   operator pfn() { return _ptr; }
  44.   pfn operator *() { return _ptr; }
  45.   void operator ()(T1 arg1) { _ptr(arg1); }
  46.   pfn1<T1> &operator =(pfn ptr) { _ptr = pfn; return *this; }
  47. private:
  48.   pfn _ptr;
  49. };
  50.  
  51. template <class T1, class T2>
  52. class pfn2
  53. {
  54. public:
  55.   typedef void (*pfn)(T1, T2);
  56.   pfn2() {}
  57.   pfn2(pfn ptr) : _ptr(ptr) {}
  58.   operator pfn() { return _ptr; }
  59.   pfn operator *() { return _ptr; }
  60.   void operator ()(T1 arg1, T2 arg2) { _ptr(arg1, arg2); }
  61.   pfn2<T1,T2> &operator =(pfn ptr) { _ptr = pfn; return *this; }
  62. private:
  63.   pfn _ptr;
  64. };
  65.  
  66. So pfn2<int,char**> will replace void (*)(int,char**)
  67.  
  68. I'm not sure it's worth the effort.
  69.  
  70.  Frederic LACHASSE (ECP 86)
  71.  CompuServe: 100530,2005
  72.  Internet: lachass@worldnet.fr
  73.  
  74.